Xbasic

SAVE_TO_FILE Function

Syntax

L save_to_file(C data ,C fileLocation [,L appendToFile [,L silent ]])

Arguments

dataCharacter

The data to save to file.

fileLocationCharacter

The location to save the file.

The location is either a fully qualified path on the server that executes the Xbasic script or a JSON string with the following properties:

connectionStringCharacter

The name of a saved Storage Connection.

objectNameCharacter

The name of the file object to create that contains the saved data.

appendToFileLogical

Default = .f.. If .t., data will be appended to the file. If .f., file will be overwritten.

silentLogical

Default = .t.. If .t., suppresses all popup messages. If .f., displays any popup messages that occur when the file is saved.

Returns

ResultLogical

Returns .t. if the data is saved. Otherwise, returns .f..

Description

Save a string to a file. The file can be saved to the local machine or Amazon S3.

Discussion

SAVE_TO_FILE() saves a character string to a file. If the file exists, data can optionally be appended to the file. The file can be a file on the server or a file on AmazonS3 by specifying a Storage Connection string and object name in the file location parameter as a JSON string.

Example: Saving text to a file

This script saves text to a file.

dim text as c = "Example saving text to a file."
dim fileLocation as c = A5.GET_PATH() + "simpleExample.txt"

if (save_to_file(text, fileLocation) == .f.) then
    '' An error occurred
end if

Example: Appending Information to a file

This script demonstrates how to append text to a file.

dim moreText as c = crlf()+"Appending data to an existing file."
dim fileLocation as c = A5.GET_PATH() + "simpleExample.txt"
dim appendToFile as L = .t.

if (save_to_file(moreText, fileLocation, appendToFile) == .f.) then
    '' An error occurred
end if

Example: Saving text to a file on AmazonS3

This script demonstrates how to write to a file on AmazonS3 using a Storage Connection.

dim text as c = "Example saving text to a file using a Storage Connection."
dim fileSaveParams as P
fileSaveParams.connectionString = "AmazonS3" 'The name of a saved Storage Connection string
fileSaveParams.objectName = "logs\myFile.txt" 'The name of the object to create

' Generate a JSON string:
dim containsSpecialFlags as L = .f.
dim compactJSON as L = .t.

dim fileLocation as C = json_generate(fileSaveParams,containsSpecialFlags,compactJSON)

if (save_to_file(text, fileLocation) == .f.) then
    '' An error occurred
end if

To append data to a file on AmazonS3, set the appendToFile parameter to .t. when calling save_to_file().

See Also